package jsp;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 * @date 2023年07月04日 11:23
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path){
            case "/add.session":
                addSession(request,response);
                break;
            case "/find.session":
                findSession(request,response);
                break;
            case "/modify.session":
                modifySession(request,response);
                break;
            case "/remove.session":
                removeSession(request,response);
                break;
            case "/clear.session":
                clearSession(request,response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>您输入的请求尚未处理,请检查请求是否正确<h1>");
                break;

        }
    }

    protected void addSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setAttribute("msg","Hello Session");
        Emp emp = new Emp();
        emp.setId(1);
        emp.setName("管理员");
        emp.setSalary(6000.0);
        session.setAttribute("emp",emp);
    }

    protected void findSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String msg = (String) session.getAttribute("msg");
        System.out.println(msg);
        Emp emp = (Emp) session.getAttribute("emp");
        System.out.println(emp.getId()+"--"+emp.getName()+"--"+emp.getSalary());
    }

    protected void modifySession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 修改本质上也是添加,只要key相同则覆盖
        HttpSession session = request.getSession();
        // 设置session的生存时间,单位:秒
        // 默认情况下,session的生存时间是30分钟
        // 正式开发中,一般不建议修改生存时间
        session.setMaxInactiveInterval(3600);
        // 如果生存时间设置为0,不是表示删除,而是表示永不过期
        session.setMaxInactiveInterval(0);
        session.setAttribute("msg","Hello World");
        Emp emp = new Emp();
        emp.setId(2);
        emp.setName("张三");
        emp.setSalary(5000.0);
        session.setAttribute("emp",emp);
    }

    protected void removeSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在session中提供了删除方法
        // 方式一:删除指定key的数据
        request.getSession().removeAttribute("msg");
    }

    protected void clearSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 方式二:清空所有session
        // 该方式本质上不是清空
        // 而是销毁了原有的session对象,重新创建了一个新的对象
        request.getSession().invalidate();
    }
}
